/**
*  项目 webpack配置
*  author: Junr
*  version: 2.0.0
*/
var webpack = require('webpack'),
    path = require('path'),
    config = require('./src/config.js'),                        //全避配置
    HtmlWebpackPlugin = require('html-webpack-plugin'),         //模板插件
    ExtractTextPlugin = require("extract-text-webpack-plugin"), //Css分割插件
    CleanWebpackPlugin = require('clean-webpack-plugin'),       //删除插件
    NgAnnotatePlugin = require('ng-annotate-webpack-plugin'),   //自动注入注解插件
    CopyWebpackPlugin = require('copy-webpack-plugin'),         //复制文件和文件夹
    autoprefixer = require('autoprefixer'),
    buildPath = path.resolve(__dirname, "build"),               //发布目录
    __DEV__ = process.env.npm_lifecycle_event === 'build',      //发布环境判断
    publicPath = '',
    devtool = '#source-map',                                     //source-map模式
    plugins = [
        new webpack.ProvidePlugin({
            jQuery: "jquery",
            $: "jquery",
            "window.jQuery": "jquery",
        }),
        new HtmlWebpackPlugin({
            chunks: ['app', 'vendor'],
            template: __dirname + '/src/index.html',
            filename: './index.html'
        }),
        new webpack.optimize.CommonsChunkPlugin('vendor', '/js/lib/vendor.[hash:6].js'),
        new ExtractTextPlugin("/assets/css/[name].[hash:6].css"),
        new NgAnnotatePlugin({
            add: true
        })
    ],
    ROOT = path.resolve(__dirname, '.');

//根目录的获取    
function root(args) {
  args = Array.prototype.slice.call(arguments, 0);
  return path.join.apply(path, [ROOT].concat(args));
}
if (__DEV__) {
    //压缩
    plugins.push(
        new CleanWebpackPlugin('build', {
            verbose: true,
            dry: false
        }),
        new webpack.HotModuleReplacementPlugin(),
        new webpack.optimize.UglifyJsPlugin({
            compress: {
                warnings: false,
                drop_debugger: true,   
                drop_console: true   //移除console
            }
        }),
        //复制include html模板
        new CopyWebpackPlugin([{
            to: root('./build/include'),
            from: root('./src/include')
        }]),
        //复制 ueditor
        new CopyWebpackPlugin([{
            to: root('./build/plugins/ueditor'),
            from: root('./src/plugins/ueditor')
        }]),
        //复制 JSON文件
        new CopyWebpackPlugin([{
            to: root('./build/assets/json'),
            from: root('./src/assets/json')
        }])
    );
}

var config = {
     resolve: {
        //别名配置
        alias: {
            '~src': path.resolve(__dirname, './src/'),
            '~plugins': path.resolve(__dirname, './src/plugins/'),
            '~view':path.resolve(__dirname, './src/view/'),
            '~directive':path.resolve(__dirname, './src/directive/'),
            '~json':path.resolve(__dirname, './src/assets/json'),
            '~server':path.resolve(__dirname, './src/server/'),
            '~assets':path.resolve(__dirname, './src/assets/'),
            '~include':path.resolve(__dirname, './src/include/')
        }
    },
    //入口文件配置
    entry: {
        app: path.resolve(__dirname, 'src/app.js'),
        vendor: ["angular", 'angular-ui-router', 'oclazyload','jquery','bootstrap','angular-ui-bootstrap']
    },
    //文件导出的配置
    output: {
        path: buildPath,
        filename: "js/lib/[name].[hash:6].js",
        publicPath: publicPath,
        chunkFilename: "js/chunks/[name].chunk.[chunkhash].js"
    },
    //模块配置
    module: {
        loaders: [
            {
                test: /\.js$/, 
                loaders: ['babel-loader'],
                exclude: [
                    root('./node_modules/angular'),
                    root('./node_modules/angular-ui-router'),
                    root('./node_modules/jquery'),
                    root('./src/plugins/layer'),
                    root('./node_modules/webuploader')
                ]
            },
            {
                test: /\.html$/,
                loader: 'raw'
            }, 
            {
                test: /\.(png|jpg|gif)$/,
                loader: 'url?limit=8192,name=/assets/images/[name].[hash:6].[ext]'
            }, 
            {
                test: /\.(woff|woff2|eot|ttf|svg)(\?.*$|$)/,
                loader: 'url-loader?importLoaders=1&limit=1000&name=/assets/fonts/[name].[ext]'
            }, 
            {
                test: /\.less$/,
                loader: ExtractTextPlugin.extract("css!postcss!less")
            },
            {
                test   : /\.css$/,
                loader: ExtractTextPlugin.extract("style-loader","css-loader")
            }
        ]
    },
    postcss: function() {
        return [autoprefixer()];
    },
    //插件配置
    plugins: plugins,
    devServer: {
        contentBase: 'src',
        colors: true,
        host: config.DEV_HOST,
        disableHostCheck: true,
        historyApiFallback: true,
        port: config.DEV_API_PORT
    },
    //调试配置
    //devtool:devtool
}
//开启代理，如果接口处理过跨域，可以不配置代理转发api
if(config.DEV_PROXY_API){
    devServer.proxy = {
         '/api': config.DEV_PROXY_API
    }
}
module.exports = config;
